﻿using NHibernateDemo.DAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using NHibernate;
using NHibernateDemo.DAL.Entity;
using System.Collections.Generic;
using System.IO;
using Arich.Data.GenericNHDal;
namespace NHibernateDemo.DAL.Test
{
    
    
    /// <summary>
    ///This is a test class for PersonBasicDALTest and is intended
    ///to contain all PersonBasicDALTest Unit Tests
    ///</summary>
    [TestClass( )]
    public class PersonBasicDALTest {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext {
            get {
                return testContextInstance;
            }
            set {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestInitialize]
        public void Setup( ) {
            log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize( ); 
        }

        /// <summary>
        ///A test for PersonBasicDAL Constructor
        ///</summary>
        //[TestMethod( )]
        //public void PersonBasicDALConstructorTest( ) {
        //    ISession session = null; // TODO: Initialize to an appropriate value
        //    PersonBasicDAL target = new PersonBasicDAL();
           
        //    //Assert.Inconclusive("TODO: Implement code to verify target");
        //}

        /// <summary>
        ///A test for GetById
        ///</summary>
        [TestMethod( )]
        public void GetByIdTest( ) {
          
            PersonBasicDAL target = new PersonBasicDAL(); // TODO: Initialize to an appropriate value
           
            PersonBasic expected = new PersonBasic { Age = 10, Name = new Name { FirstName = "123", LastName = "sss" } }; // TODO: Initialize to an appropriate value
            PersonBasic actual;
            target.Save(expected);
            actual = target.GetById(expected.PersonId);
            Assert.AreEqual(expected.Age, actual.Age);
            target.Delete(expected);
            Assert.IsNull(target.GetById(expected.PersonId));
            
            //Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for CreationPerson
        ///</summary>
        [TestMethod( )]
        public void CreationPersonTest( ) {
         
            PersonBasicDAL target = new PersonBasicDAL(); // TODO: Initialize to an appropriate value
            PersonBasic person = new PersonBasic { Age = 10, Name = new Name { FirstName = "123", LastName = "sss" } }; // TODO: Initialize to an appropriate value
            
            target.Save(person);
            var newP = target.GetById(person.PersonId);
            Assert.AreEqual(newP.Age, person.Age);
            target.Delete(person);
            //Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }



       

        /// <summary>
        ///A test for GetByFirstName
        ///</summary>
        [TestMethod( )]
        public void GetByFirstNameTest( ) {
           
            PersonBasicDAL target = new PersonBasicDAL(); // TODO: Initialize to an appropriate value
            string name = "123"; // TODO: Initialize to an appropriate value
            IList<PersonBasic> expected = null; // TODO: Initialize to an appropriate value
            IList<PersonBasic> actual;
            PersonBasic person = new PersonBasic { Age = 10, Name = new Name { FirstName = "123", LastName = "sss" } };
            target.Save(person);
            actual = target.GetByFirstName(name);
            Assert.AreEqual(1, actual.Count);
            target.Delete(person);
            //Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for GetByLastName
        ///</summary>
        [TestMethod( )]
        public void GetByLastNameTest( ) {
           
            PersonBasicDAL target = new PersonBasicDAL(); // TODO: Initialize to an appropriate value
            string name = "sss"; // TODO: Initialize to an appropriate value
            PersonBasic person = new PersonBasic { Age = 10, Name = new Name { FirstName = "123", LastName = "sss" } };
            IList<PersonBasic> expected = null; // TODO: Initialize to an appropriate value
            IList<PersonBasic> actual;
            target.Save(person);
            actual = target.GetByLastName(name);
            Assert.AreEqual(1, actual.Count);
            target.Delete(person);
            //Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for GetByFirstNameAndAge
        ///</summary>
        [TestMethod( )]
        public void GetByFirstNameAndAgeTest( ) {
     
            PersonBasicDAL target = new PersonBasicDAL(); // TODO: Initialize to an appropriate value
            string name = "zx"; // TODO: Initialize to an appropriate value
            int age = 5; // TODO: Initialize to an appropriate value
            IList<PersonBasic> expected = null; // TODO: Initialize to an appropriate value
            IList<PersonBasic> actual;
            var person = new PersonBasic { Age = 5, Name = new Name { FirstName = "zxc", LastName = "asd" } };
            target.Save(person);

            actual = target.GetByFirstNameAndAge(name, age);
            Assert.AreEqual(1, actual.Count);
            Assert.AreEqual("zxc", actual[0].Name.FirstName);
            target.Delete(person);
            //Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for Delete
        ///</summary>
        [TestMethod( )]
        public void DeleteNullTest( ) {
           
            PersonBasicDAL target = new PersonBasicDAL(); // TODO: Initialize to an appropriate value
            PersonBasic person = null; // TODO: Initialize to an appropriate value
            target.Delete(person);
            //Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }

        /// <summary>
        ///A test for SafeBatchAdd
        ///</summary>
        [TestMethod( )]
        public void SafeBatchAddTest( ) {
         
            PersonBasicDAL target = new PersonBasicDAL(); // TODO: Initialize to an appropriate value
            PersonBasic p1 = new PersonBasic { Name = new Name { FirstName = "1" } };
            PersonBasic p2 = new PersonBasic { Name = new Name { FirstName = "2" } };
            target.Save(p1);
            p1.Name.FirstName = "aaa";
            IList<PersonBasic> persons = new List<PersonBasic> { p1, p2}; // TODO: Initialize to an appropriate value
            target.SafeBatchAdd(persons);
            var existList = target.GetByFirstNameAndAge("", 0);
            Assert.AreEqual(2, existList.Count);
            Assert.AreEqual("aaa", existList[0].Name.FirstName);
            foreach (var p in existList) {
                target.Delete(p);
            }
            //Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }


        [TestMethod]
        public void SearchPersonCacheTest( ) {
            ISession session = NHibernateSessionManager.Instance.GetSession( );
            PersonBasic p1 = new PersonBasic { Name = new Name { FirstName = "111" } };
            PersonBasic p2 = new PersonBasic { Name = new Name { FirstName = "111" } };
            PersonBasicDAL target = new PersonBasicDAL();
            target.Save(p1);
            target.Save(p2);
         
            //session.SessionFactory.Evict(typeof(PersonBasic));
            target = new PersonBasicDAL( );
            var ps0 = target.GetByFirstName("111");
          
            var ps = target.GetByFirstName("111");
            
            //target.Dispose( );
            target = new PersonBasicDAL( );
            var p3 = target.GetById(p1.PersonId);
            Assert.AreEqual(p1.Name.FirstName, p3.Name.FirstName);
            var ps2 = target.GetByFirstName("111");
            //Assert.AreEqual(ps.Count, ps2.Count);
            //target.Dispose( );
            target = new PersonBasicDAL( );
            var p4 = target.GetById(p1.PersonId);
            Assert.AreEqual(p1.Name.FirstName, p4.Name.FirstName);
            var ps3 = target.GetByFirstName("111");
            p2.PersonId = 0;
            p2.Name.FirstName = "222";
            target.Save(p2);
            //target.Dispose( );
            target = new PersonBasicDAL( );
            var ps4 = target.GetByFirstName("111");
            foreach (var p in ps4) {
                target.Delete(p);
            }
            

        }
    }
}
